<template>
  <el-dialog :title="dialogProps.title" :visible.sync="dialogProps.visible" width="560px" 
     v-loading="loading">
    <div slot="title" class="dialog-header">{{ dialogProps.title }}</div>
    <el-form :model="deptModel"
      ref="roleForm" label-width="80px" label-position="right" size="small">
      <el-form-item label="姓名" prop="hName">
        <el-col :span="16"><el-input :maxlength="32" v-model="deptModel.hName"></el-input></el-col>
      </el-form-item> 
      <el-form-item label="身份证" prop="hNumber">
        <el-col :span="16"><el-input :maxlength="32" v-model="deptModel.hNumber"></el-input></el-col>
      </el-form-item>   
      <el-form-item label="年龄" prop="hAge">
        <el-col :span="16">
            <el-radio v-model="deptModel.hSex" label="男">男</el-radio>
            <el-radio v-model="deptModel.hSex" label="女">女</el-radio>
        </el-input></el-col>
      </el-form-item> 
      <el-form-item label="年龄" prop="hAge">
        <el-col :span="16"><el-input :maxlength="32" v-model="deptModel.hAge"></el-input></el-col>
      </el-form-item>  
      <el-form-item label="出生日期" prop="hDateofbirth">
        <el-col :span="16"><el-date-picker
          v-model="deptModel.hDateofbirth"
          type="date"
          placeholder="选择日期">
        </el-date-picker></el-col>
      </el-form-item> 
      <el-form-item label="手机号" prop="hPhone">
        <el-col :span="16"><el-input :maxlength="32" v-model="deptModel.hPhone"></el-input></el-col>
      </el-form-item>    
      <el-form-item label="年龄" prop="hMaritalstatus">
        <el-col :span="16">
            <el-radio v-model="deptModel.hMaritalstatus" label="已婚">已婚</el-radio>
            <el-radio v-model="deptModel.hMaritalstatus" label="未婚">未婚</el-radio>
        </el-input></el-col>
      </el-form-item>
      <el-form-item label="体检日期" prop="deptName">
        <el-col :span="16"><el-date-picker
          v-model="Order.orderdate"
          type="date"
          format=yyyy-MM-dd
          value-format=yyyy-MM-dd
          placeholder="选择日期"
          :picker-options="pickerOptions0">
        </el-date-picker></el-col>
      </el-form-item> 
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleDialogClose()">取 消</el-button>
      <el-button type="primary" size="small" @click="doUpdateRole()">确 定</el-button>
    </span>    
  </el-dialog>
</template>

<script>
import { getMemberById } from '@/api/HMemberofthefile'
import { saveOrder } from '@/api/torder'

export default {
  name: 'dept-edit',
  data() {
    return {
      pickerOptions0: {
          disabledDate(time) {
            return time.getTime() < Date.now() - 8.64e7;//如果没有后面的-8.64e7就是不可以选择今天的 
         }
        },  
      loading: false,
      deptModel: {
        hId: "",
        hFn: "",
        hName: "",
        hSex: "",
        hAge: "",
        hNumber: "",
        hPhone: "",
        hRegistration: "",
        hPassword: "",
        hNation: "",
        hDateofbirth: "",
        hMailbox: "",
        hMaritalstatus: "",
        hOccupation: "",
        hStandard: "",
        hThehealthmanager: "",
        hFilestatus: "",
        hRemark: "",
        hHeight: "",
        hWeight: "",
        hDiastolic: "",
        hSystolic: "",
        hRespiratoryfrequency: "",
        hAnimalheat: "",
        hWaistline: "",
        hHipline: "",
        hBmi: "",
        hOxyhemoglobin: "",
        hPrevious: "",
        hFamilyhistory: "",
        hAllergy: "",
        hMedicalhistory: "",
        hChronicdisease: "",
      },
      dialogProps: {
        visible: false,
        action: '',
        title: ''
      },
      Order:{
        memberId:"",
        orderdate:"",
        ordertype:"",
        orderstatus:"",
        setmealId:"",
        hMemberofthefile:{
          hName: "",
          hSex: "",
          hAge: "",
          hNumber: "",
          hPhone: "",
          hMaritalstatus:""
        }
      }
    }    
  },
  methods: {
    doUpdateRole() {
      this.Order.hMemberofthefile.hName=this.deptModel.hName
      this.Order.hMemberofthefile.hSex=this.deptModel.hSex
      this.Order.hMemberofthefile.hAge=this.deptModel.hAge
      this.Order.hMemberofthefile.hNumber=this.deptModel.hNumber
      this.Order.hMemberofthefile.hPhone=this.deptModel.hPhone
      this.Order.hMemberofthefile.hMaritalstatus=this.deptModel.hMaritalstatus
      saveOrder(this.Order)
      .then((response) => {
        if(response.data.hId==1000){
          console.log("预约成功");
          this.loading = false;
          this.dialogProps.visible = false;
          this.$emit("save-finished");
          this.$message({
            showClose: true,
            message: "预约成功",
            type: "success",
          });
          return
        }
        if(response.data.hId==1001){
          this.loading = false;
          this.dialogProps.visible = false;
          this.$emit("save-finished");
          this.$message({
            showClose: true,
            message: "已经预约过不能重复预约",
            type: "error",
          });
          return
        }
        if(response.data!=null){
          console.log(response);
          sessionStorage.setItem("is_login", response.data.hName);
          sessionStorage.setItem("is_loginID", response.data.hId);
          sessionStorage.setItem("fn", response.data.hFn);
          this.loading = false;
          this.dialogProps.visible = false;
          this.$emit("save-finished");

          this.$message({
            showClose: true,
            message: "预约成功",
            type: "success",
          });

          return
        }
          if(response.data==null){
            this.$message({
            showClose: true,
            message: "预约失败",
            type: "error",
          });
          }
        })
        .catch((error) => {
          this.outputError(error);
        });
    },
    handleDialogClose() {
      this.dialogProps.visible = false  
    },
    outputError(error) {
      console.log(error.response ? error.response : error.message);
      this.loading = false;
      this.$message({
        showClose: true,
        message: "出错了，请按F12查看浏览器日志。",
        type: "error",
      });
    },   
  },
  created: function() {
    this.$nextTick(() => {  
      this.$on('openEditDeptDialog', function(role) {
        this.Order.memberId=sessionStorage.getItem('is_loginID')
        this.Order.setmealId=role
        this.Order.ordertype="网站预约"
        this.Order.orderstatus="未到诊"
        this.dialogProps.action = 'edit'
        this.dialogProps.title = '预约'
        this.dialogProps.visible = true

        var id = sessionStorage.getItem('is_loginID')
        console.log(id)
        if(id !=null && id!=""){
          getMemberById(id)
            .then(response => {
              console.log(response.data)
              this.deptModel=response.data
          })
          .catch(error => {
            this.outputError(error)        
          })
        }
      })
    })
  }  
}
</script>

